package com.base.service.business;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.base.mapper.product.ProductCcieDataMapper;
import com.base.mapper.product.ProductDataMapper;
import com.base.mapper.product.ProductGoldDataMapper;
import com.base.mapper.product.ProductSaleDataMapper;
import com.base.mapper.product.ProductSettleAccountDataMapper;
import com.base.mapper.product.ProductStoneDataMapper;
import com.base.model.product.ProductCcieDataItem;
import com.base.model.product.ProductDataItem;
import com.base.model.product.ProductDto;
import com.base.model.product.ProductGoldDataItem;
import com.base.model.product.ProductSaleDataItem;
import com.base.model.product.ProductSettleAccountDataItem;
import com.base.model.product.ProductStoneDataItem;

@Service
public class ProductDataService {

	@Autowired
	private ProductDataMapper productDataMapper;
	@Autowired
	private ProductGoldDataMapper productGoldDataMapper;
	@Autowired
	private ProductStoneDataMapper productStoneDataMapper;
	@Autowired
	private ProductSaleDataMapper productSaleDataMapper;
	@Autowired
	private ProductCcieDataMapper productCcieDataMapper;
	@Autowired
	private ProductSettleAccountDataMapper productSettleAccountDataMapper;

	@Transactional(propagation = Propagation.REQUIRES_NEW)
	void save(ProductDto dto) {
		// 保存单品主数据
		ProductDataItem productDataItem = dto.getProductDataItem();
		productDataMapper.insert(productDataItem);
		// 保存单品金料数据
		List<ProductGoldDataItem> goldDataItems = dto.getProductGoldDataItem();
		if (!CollectionUtils.isEmpty(goldDataItems)) {
			for (ProductGoldDataItem goldItem : goldDataItems) {
				productGoldDataMapper.insert(goldItem);
			}
		}
		// 保存单品石料数据
		List<ProductStoneDataItem> stoneDataItems = dto.getProductStoneDataItem();
		if (!CollectionUtils.isEmpty(stoneDataItems)) {
			for (ProductStoneDataItem stoneItem : stoneDataItems) {
				productStoneDataMapper.insert(stoneItem);
			}
		}
		// 保存单品代销数据
		List<ProductSettleAccountDataItem> settleAccountDataItems = dto.getProductSettleAccountDataItem();
		if (!CollectionUtils.isEmpty(settleAccountDataItems)) {
			for (ProductSettleAccountDataItem settleAccountItem : settleAccountDataItems) {
				productSettleAccountDataMapper.insert(settleAccountItem);
			}
		}
		// 保存单品证书数据
		List<ProductCcieDataItem> ccieDataItems = dto.getProductCcieDataItem();
		if (!CollectionUtils.isEmpty(ccieDataItems)) {
			for (ProductCcieDataItem ccieItem : ccieDataItems) {
				productCcieDataMapper.insert(ccieItem);
			}
		}
		// 保存单品销售数据
		List<ProductSaleDataItem> saleDataItems = dto.getProductSaleDataItem();
		if (!CollectionUtils.isEmpty(saleDataItems)) {
			for (ProductSaleDataItem saleItem : saleDataItems) {
				productSaleDataMapper.insert(saleItem);
			}
		}
	}

}
